package com.small.common;

import com.small.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @create: xie
 */
@Component
@Slf4j
public class RedissonManager {

    private Config config=new Config();

    private Redisson redisson;

    public Redisson getRedisson() {
        return redisson;
    }

    private static Integer port1 = PropertiesUtil.getIntegerProperty("redis1.port");//redis端口
    private static String ip1 = PropertiesUtil.getProperty("redis1.ip");//redisIp
    private static Integer port2 = PropertiesUtil.getIntegerProperty("redis2.port");//redis端口
    private static String ip2 = PropertiesUtil.getProperty("redis2.ip");//redisIp

    //被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器执行一次
    @PostConstruct
    private void init(){
        try {
            config.useSingleServer().setAddress(ip1+":"+port1);
            redisson= (Redisson) Redisson.create(config);
            log.info("初始化Redisson结束");
        } catch (Exception e) {
            log.error("Redisson error",e);
        }
    }
}
